/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年11月16日 下午9:34:06
 */
package com.absir.shoumeng.service;

import com.absir.appserv.configure.JConfigureUtils;
import com.absir.appserv.system.bean.proxy.JiUserBase;
import com.absir.appserv.system.helper.HelperClient;
import com.absir.appserv.system.helper.HelperJson;
import com.absir.appserv.system.service.IdentityService;
import com.absir.appserv.system.service.SecurityService;
import com.absir.bean.basis.Base;
import com.absir.bean.core.BeanFactoryUtils;
import com.absir.bean.inject.value.Bean;
import com.absir.shoumeng.config.JShouMengConfig;

/**
 * @author absir
 *
 */
@Base
@Bean
public class ShouMengService implements IdentityService {

	/** ME */
	public static final ShouMengService ME = BeanFactoryUtils.get(ShouMengService.class);

	/** CONFIG */
	public static final JShouMengConfig CONFIG = JConfigureUtils.getConfigure(JShouMengConfig.class);

	/** PLATFORM_NAME */
	public static final String PLATFORM_NAME = "ShouMeng";

	/** sessionUrl */
	protected static final String sessionUrl = "http://www.19meng.com/api/v1/verify_session_id";

	protected static class SessionData {

		public String user_id;

		public String session_id;

	}

	protected static class SessionResult {

		public int result;

		public String message;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.IdentityService#getUserBase(java.lang
	 * .String[])
	 */
	@Override
	public JiUserBase getUserBase(String[] parameters) {
		if (parameters.length > 2) {
			SessionData sessionData = new SessionData();
			sessionData.user_id = parameters[1];
			sessionData.session_id = parameters[2];
			SessionResult result = HelperClient.openConnection(sessionUrl, true, HelperJson.encodeNull(sessionData).getBytes(),
					SessionResult.class);
			if (result != null && result.result == 1) {
				return SecurityService.ME.openUserBase(sessionData.user_id, null, PLATFORM_NAME);
			}
		}

		return null;
	}
}
